package com.rocksdata.mc.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.ctz.commonframe.utils.ApplicationUtil;
import com.ctz.commonframe.utils.Logger;
import com.rocksdata.mc.MobileCampusApplication;
import com.rocksdata.mc.business.UserInfoManager;

public class DatabaseManager{
    private static DatabaseHelper helper;

	public static UserInfoDBManager getUserInfoDBManager() {
		return UserInfoDBManager.getInstance();
	}

	public static ApplicationInfoDBManager getApplicationInfoDBManager() {
		return ApplicationInfoDBManager.getInstance();
	}

	public static TimetableInfoDBManager getTimetableInfoDBManager() {
		return TimetableInfoDBManager.getInstance();
	}

    public static AnnouncementDBManager getAnnouncementDBManager() {
        return AnnouncementDBManager.getInstance();
    }

    public static PayrollDBManager getPayrollDBManager(){
        return PayrollDBManager.getInstance();
    }

    public static TranscriptDBManager getTranscriptDBManager(){
        return TranscriptDBManager.getInstance();
    }

    public static void initDb() {
        Context context = MobileCampusApplication.getContext();
        String databaseName = "user_" + UserInfoManager.getCurrentUserId() + ".db";
        Logger.e("init database, database name:" + databaseName);
        assert context != null;
        helper = new DatabaseHelper(context, databaseName, ApplicationUtil.getAppVersionCode(context));
    }

	public static SQLiteDatabase getDatabase(){
	    if (helper == null)
	        initDb();
        return helper.getWritableDatabase();
    }

    public static void closeDb() {
	    Logger.e("close db");
	    if (helper != null) {
	        Logger.e("close db, db name:" + helper.getDatabaseName());
            helper.close();
            helper = null;
        }
    }
}
